package com.ace.user.api.restful;


import com.ace.user.api.annotation.AuthIgnore;
import com.alibaba.fastjson.JSONObject;
import com.ace.user.common.bean.RequestDTO;
import com.ace.user.common.bean.ResponseDTO;
import com.ace.user.common.bean.dto.UserDTO;
import com.ace.user.common.constant.Group;
import com.ace.user.common.constant.R;
import com.ace.user.module.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户定义表 前端控制器
 * </p>
 *
 * @author jobObj
 * @since 2019-10-11
 */
@RestController
@RequestMapping("api/v1/user")
public class UserController {
    @Autowired
    private IUserService userService;

    @AuthIgnore
    @PutMapping("/register")
    public Integer add(@RequestBody @Validated({Group.Default.class,Group.Add.class})RequestDTO<UserDTO> requestDTO){
        Integer id =  userService.add(requestDTO.getData());
        return id;
    }

    @DeleteMapping("/delUser")
    public JSONObject delUser(@RequestBody @Validated({Group.Delete.class})RequestDTO<UserDTO> requestDTO){
        boolean ret =  userService.delUser(requestDTO.getData());
        JSONObject data = new JSONObject();
        data.put("ret", ret);
        return data;
    }

    @GetMapping("/onLineUser/{userId}")
    public ResponseDTO onLineUser(@PathVariable(value="userId")String userId, HttpServletRequest request){
        List<Map<String,Object>> onLineUserList=  userService.onLineUser(request.getHeader("appid"),userId);
        ResponseDTO responseDTO = R.OK.copy();
        JSONObject data = new JSONObject();
        data.put("list", onLineUserList);
        responseDTO.setData(data);
        return responseDTO;
    }

}
